package gw.com.android.ui.home;

import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.Calendar;

import gw.com.android.ui.dialog.CalendarPopView;
import gw.com.android.ui.dialog.FilterPopView;
import gw.com.android.ui.news.CalendarArgs;
import gw.com.android.ui.news.CalendarTabDateViewHolder;

/**
 * 日历头部参数控制处理
 * Created by hubble on 2017/4/25.
 */
public class CalendarHeadHolder implements View.OnClickListener {

    private Activity mActivity;
    private HomeCalendarPresenter mCalendarPresenter;

    private View mParent;
    private View mCalendar;
    private ImageButton ibPick;
    private RelativeLayout mRlFilter;
    private CalendarPopView mCalenarPopview;
    private FilterPopView mFilterPopview;
    private TextView mTvBackToday;
    private CalendarTabDateViewHolder mDateTabViewHolder;
    private CalendarHeadViewCallback mCallback;

    // 请求参数
    private Calendar mCurCalendar = Calendar.getInstance(); // 日期

    CalendarHeadHolder(Activity activity, HomeCalendarPresenter calendarPresenter, View parent, View calendar) {
        this.mActivity = activity;
        this.mCalendarPresenter = calendarPresenter;
        this.mParent = parent;
        this.mCalendar = calendar;
        initView();
    }

    private void initView() {
        this.ibPick = mParent.findViewById(R.id.calendar_pick);
        this.mRlFilter = mParent.findViewById(R.id.calendar_filter_bar);
        LinearLayout ll_filter = mParent.findViewById(R.id.calendar_filter);
        this.mCalenarPopview = new CalendarPopView(mCalendar);

        mCalendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCalenarPopview.isShowing()) {
                    mCalenarPopview.hide();
                }
            }
        });
        mTvBackToday = mParent.findViewById(R.id.calendar_back_today);
        hideBackToday();
        mDateTabViewHolder = new CalendarTabDateViewHolder(mActivity, mParent.findViewById(R.id.calendar_date_tab));
        mDateTabViewHolder.updateDate(mCurCalendar);
        // 日期tab变动刷新
        mDateTabViewHolder.setOnSelectedDateListener(new CalendarTabDateViewHolder.OnSelectedDateListener() {
            @Override
            public void onSelectedDate(Calendar date) {
                // 更新日历控件
                mCalenarPopview.gotoCurrentDay(date);
                // 更新当前选择的日期
                mCurCalendar = date;

                if (mCallback != null) {
                    mCallback.onRefresh(date);
                }
            }
        });

        // 日期选择变动刷新
        this.mCalenarPopview.setCallback(new CalendarPopView.CalendarCallback() {

            @Override
            public void onSelectedDate(Calendar date) {
                calendarSelected(date);
            }

            @Override
            public void onCurDayVisibilityChanged(boolean visible) {
                if (visible) {
                    hideFilterView();
                    hideBackToday();
                } else {
                    showBackToday();
                }
            }

            @Override
            public void onDismiss() {
                showFilterView();
            }

        });


        mTvBackToday.setOnClickListener(this);
        this.ibPick.setOnClickListener(this);
        ll_filter.setOnClickListener(this);
    }

    private void calendarSelected(Calendar date) {
        // 更新
        mCurCalendar = date;
        mDateTabViewHolder.updateDate(date);
        showFilterView();
    }

    private void showFilterView() {
        hideBackToday();
        mRlFilter.setVisibility(View.VISIBLE);
    }

    private void hideFilterView() {
        mRlFilter.setVisibility(View.INVISIBLE);
    }

    private void showBackToday() {
        hideFilterView();
        mTvBackToday.setVisibility(View.VISIBLE);
    }

    private void hideBackToday() {
        mTvBackToday.setVisibility(View.INVISIBLE);
    }

    public void setCallback(CalendarHeadViewCallback callback) {
        this.mCallback = callback;
    }

    public void resetFilterPopup() {
        mFilterPopview = null;
    }

    private FilterPopView createFilterPopup() {
        CalendarArgs filterArgs = mCalendarPresenter.getFilterArgs();
        FilterPopView filterPopView = new FilterPopView(mActivity, filterArgs);
        // 过滤参数变动刷新
        filterPopView.setCallback(new FilterPopView.FilterCallback() {
            @Override
            public void onConfirm(CalendarArgs args) {
                if (mCallback != null) {
                    mCallback.onFilter(args);
                }
            }
        });
        return filterPopView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.calendar_filter:
                if (mFilterPopview == null) {
                    mFilterPopview = createFilterPopup();
                }
                mFilterPopview.show(mParent);
                break;
            case R.id.calendar_pick:
                if (mCalenarPopview.isShowing()) {
                    mCalenarPopview.hide();
                } else {
                    hideFilterView();
                    mCalenarPopview.show(ibPick, mCurCalendar);
                    // 如果今天不在日历控件的当前月份中，则显示回到今天
                    if (!mCalenarPopview.isInMonth(Calendar.getInstance(), mCurCalendar)) {
                        showBackToday();
                    } else {
                        hideBackToday();
                    }
                }
                break;
            case R.id.calendar_back_today:
                calendarSelected(Calendar.getInstance());
                break;
            default:
                break;
        }
    }

    boolean onBackPressed() {
        if (mCalenarPopview != null && mCalenarPopview.isShowing()) {
            mCalenarPopview.hide();
            return true;
        }
        return false;
    }

    public interface CalendarHeadViewCallback {

        void onRefresh(Calendar date);

        void onFilter(CalendarArgs args);

    }


}
